﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace sign_recognition
{
    public partial class WebCamForm : Form
    {
        public FuzzyART fuzzyART;
        public Double betha = 1;
        public Double alpha = 0.01;
        public Double rho = 0.9;

        private WebCam_Capture.WebCamCapture WebCamCapture;
        public WebCamForm()
        {
            InitializeComponent();
            // 
            // WebCamCapture
            // 
            this.WebCamCapture = new WebCam_Capture.WebCamCapture();
            this.WebCamCapture.CaptureHeight = 240;
            this.WebCamCapture.CaptureWidth = 320;
            this.WebCamCapture.FrameNumber = ((ulong)(0ul));
            this.WebCamCapture.Location = new System.Drawing.Point(17, 17);
            this.WebCamCapture.Name = "WebCamCapture";
            this.WebCamCapture.Size = new System.Drawing.Size(342, 252);
            this.WebCamCapture.TabIndex = 0;
            this.WebCamCapture.TimeToCapture_milliseconds = 100;
            this.WebCamCapture.ImageCaptured += new WebCam_Capture.WebCamCapture.WebCamEventHandler(this.WebCamCapture_ImageCaptured);
        }
        /// <summary>
        /// An image was capture
        /// </summary>
        /// <param name="source">control raising the event</param>
        /// <param name="e">WebCamEventArgs</param>
        private void WebCamCapture_ImageCaptured(object source, WebCam_Capture.WebcamEventArgs e)
        {
            // set the picturebox picture
            this.pictureBox1.Image = e.WebCamImage;
        }
        private void WebCamForm_Load(object sender, EventArgs e)
        {
            fuzzyART = new FuzzyART(320*240, true, this.alpha, this.rho, this.betha);
        }

        private void webCamButton_Click(object sender, EventArgs e)
        {
          
        }

        private void startCamButton_Click(object sender, EventArgs e)
        {
            // change the capture time frame
            this.WebCamCapture.TimeToCapture_milliseconds = (int)this.numericUpDown.Value;

            // start the video capture. let the control handle the
            // frame numbers.
            this.WebCamCapture.Start(0);
            this.classifyButton.Enabled = true;
            this.learnButton.Enabled = true;
        }

        private void StopButton_Click(object sender, EventArgs e)
        {
            // stop the video capture
            this.classifyButton.Enabled = false;
            this.learnButton.Enabled = false;
            this.WebCamCapture.Stop();
        }

        private void continueButton_Click(object sender, EventArgs e)
        {
            // change the capture time frame
            this.WebCamCapture.TimeToCapture_milliseconds = (int)this.numericUpDown.Value;

            // resume the video capture from the stop
            this.WebCamCapture.Start(this.WebCamCapture.FrameNumber);
        }
        private void captureSnapshot() {
            Bitmap snapshotBitmap = new Bitmap(pictureBox1.Image);
            ToolBox tool = new ToolBox();
            SnapshotPictureBox.Image = tool.MakeGrayscale(snapshotBitmap);
          //  snapshotBitmap.Save("C:/Users/lucachaco/Desktop/noNoise.bmp");
        }
        private void SnapshotButton_Click(object sender, EventArgs e)
        {
            if (comboBoxID.SelectedItem!=null)
            {
                this.captureSnapshot();
                fuzzyART.propagateNewInput(this.getInput(), this.comboBoxID.SelectedItem.ToString());
                this.classifiedTextBox.Visible = false;
                this.stateLabel.Visible = false;
            }
            else {
                MessageBox.Show("Favor seleccione una categoría");
            }
        

        }
        public Double[] getInput()
        {
            int index = 0;
            int total = SnapshotPictureBox.Image.Height * SnapshotPictureBox.Image.Width;
            Double[] entrada = new Double[total];

            for (int y = 0; y < SnapshotPictureBox.Image.Height; y++)
            {
                for (int x = 0; x < SnapshotPictureBox.Image.Width; x++)
                {
                    entrada[index] = (((Bitmap)SnapshotPictureBox.Image).GetPixel(x, y).R) / 255.0;
                    index++;
                }
            }
            return entrada;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.captureSnapshot();
            this.classifiedTextBox.Text = fuzzyART.clasify(this.getInput()).ToString();
            this.classifiedTextBox.Visible = true;
            this.stateLabel.Visible = true;
            
        }

        private void SaveWeightsButton_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            if (null!=saveFileDialog1.FileName)
            {
                this.fuzzyART.saveWeights(saveFileDialog1.FileName);
            }
        }



        private void guardarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            if ("" != saveFileDialog1.FileName)
            {
                this.fuzzyART.saveWeights(saveFileDialog1.FileName);
            }
        }

        private void cargarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.ShowDialog();
            if ("" != openFileDialog.FileName)
            {
                
                this.fuzzyART = new FuzzyART(320 * 240, true, this.alpha, this.rho, this.betha, openFileDialog.FileName);
            }
        }

        private void saveAsbutton_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            if (saveFileDialog1!=null)
            {
                ((Bitmap)SnapshotPictureBox.Image).Save(saveFileDialog1.FileName);
            }
        }

        private void snapshotButton_Click_1(object sender, EventArgs e)
        {
            this.captureSnapshot();
        }
    }
}
